import os

FILE_PATH = r'C:\Users\x2606\Desktop\JSON2png'
result_path = os.path.join(os.path.dirname(FILE_PATH), 'result')  # 结果文件夹路径
try:
    os.mkdir(result_path)  # 创建结果文件夹
except FileExistsError:
    print('文件夹已存在!')


# 文件复制
def file_copy(file_path, target_path):
    with open(file_path, 'rb') as f1:
        with open(target_path, 'wb') as f2:
            f2.write(f1.read())


res = os.walk(FILE_PATH)  # 获取JSON2png的所有目录和文件列表
for root, dir_list, file_list in res:
    for file_name in file_list:
        if 'mask' not in file_name:
            continue
        print(f'找到文件{file_name}')
        file_abspath = os.path.join(root, file_name)  # 文件绝对路径
        target_file_abspath = os.path.join(result_path, file_name)  # 复制后的文件绝对路径
        file_copy(file_abspath, target_file_abspath)  # 拷贝文件
        print(f'成功复制文件{file_name}到目标文件夹')
